//
//  WWActivitiesView.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/19.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWActivitiesView.h"
#import "WWActiveCell.h"
#import "WWFreshSellCell.h"

@interface WWActivitiesView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;

//
@property (nonatomic, strong)NSArray *outFreshArr;

@end

@implementation WWActivitiesView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:UITableViewStyleGrouped]) {
//        self.style = UITableViewStyleGrouped;
//        self.rowHeight = 150;
        self.dataSource = self;
        self.delegate = self;
        
    }
    return self;
}
#pragma
#pragma mark -setter 方法
- (void)setActivities:(NSArray<WWActiveModel *> *)activities{
    _activities = activities;
    [self reloadData];
}
- (void)setFreshInfo:(NSArray<WWFreshSellModel *> *)freshInfo{
    _freshInfo = freshInfo;
    
    // 对数据进行2维处理
    NSMutableArray *mOutArr = [NSMutableArray array];
    [_freshInfo enumerateObjectsUsingBlock:^(WWFreshSellModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx%2 == 0) {
            NSMutableArray *mArr = [NSMutableArray array];
            [mArr addObject:obj];
            [mOutArr addObject:mArr];
        }
    }];
    
    // 再次遍历
    [freshInfo enumerateObjectsUsingBlock:^(WWFreshSellModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx%2 == 1) {
            [mOutArr[idx/2] addObject:obj];
        }
    }];
    
    self.outFreshArr = mOutArr.copy;
    
    [self reloadData];
}

#pragma mark - 数据源 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.activities.count;
    }else {
        return self.outFreshArr.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        WWActiveModel *activeModel = self.activities[indexPath.row];
        
        WWActiveCell *cell = [[WWActiveCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:activeModel.reuseID];
        cell.imgURL = activeModel.img;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        
        WWFreshSellCell *cell = [[WWFreshSellCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"refrsheCell"];
        cell.models = self.outFreshArr[indexPath.row];
        
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

#pragma
#pragma mark - 代理 -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 150;
    }else{
        return 270;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        // 跳转到webview界面
    }else if (indexPath.section == 1){
        NSLog(@"产看商品详情");
    }
}

// MARK : - 组头文字 -
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return @"新鲜热卖";
    }else{
        return nil;
    }
}

// MARK :- 动画效果 -
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.transform = CGAffineTransformMakeTranslation(0, 100);
    
    [UIView animateWithDuration:(indexPath.row/5.0) animations:^{
        cell.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}


@end
